package com.haiyou.dc.thread.test1;

import java.util.ArrayList;
import java.util.List;

/**
 * 线程安全：同步块保证操作容器数据不丢失
 */
public class SynBlockTest02 {

    public static void main(String[] args) throws InterruptedException {

        List<String> list = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            new Thread(() -> {
                synchronized (list){
                    list.add(Thread.currentThread().getName());
                }
            }).start();
        }
        Thread.sleep(5000);
        System.out.println(list.size());

    }
}
